package com.fancy.learncenter.adapter;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Animatable;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.RelativeLayout;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeControllerBuilder;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.imagepipeline.image.ImageInfo;
import com.fancy.learncenter.adapter.base.CommonRecycleViewAdapter;
import com.fancy.learncenter.adapter.base.CustomViewHold;
import com.superservice.lya.R;

import java.util.ArrayList;

import me.relex.photodraweeview.PhotoDraweeView;

/**
 * Created by hyy on 2018/5/28.
 */

public class YgtPicGallerAdapter extends CommonRecycleViewAdapter<String> {
    int width;
    Activity mContext;

    public YgtPicGallerAdapter(Activity mContext, ArrayList<String> itemDatas) {
        super(mContext, R.layout.activity_pic_detail_item, itemDatas);
        this.mContext = mContext;
        Resources resources = mContext.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        width = dm.widthPixels;
    }

    @Override
    public void bindView(CustomViewHold customViewHold, String item, int position) {
        RelativeLayout rootView = customViewHold.getView(R.id.rootView);

        final PhotoDraweeView mPhotoDraweeView = customViewHold.getView(R.id.simpleDraweeView);

        PipelineDraweeControllerBuilder controller = Fresco.newDraweeControllerBuilder();
        controller.setUri(item);//设置图片url
        controller.setOldController(mPhotoDraweeView.getController());
        controller.setControllerListener(new BaseControllerListener<ImageInfo>() {
            @Override
            public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
                super.onFinalImageSet(id, imageInfo, animatable);
                if (imageInfo == null || mPhotoDraweeView == null) {
                    return;
                }
                mPhotoDraweeView.update(imageInfo.getWidth(), imageInfo.getHeight());
            }
        });
        mPhotoDraweeView.setController(controller.build());


//        Utils.setControllerListener(simpleDraweeView, item, width);
        mPhotoDraweeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mContext.finish();
            }
        });
    }
}
